<?php
// src/Asiah/SiklusBundle/Controller/UserController.php
namespace Asiah\SiklusBundle\Controller;

use Asiah\SiklusBundle\Entity\User;
use Asiah\SiklusBundle\Form\Type\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
                 
class UserController extends Controller {
	public function loginAction() {
		$request = $this -> getRequest();
		$session = $request -> getSession();

		// get the login error if there is one aaaaa
		if($request -> attributes -> has(SecurityContext::AUTHENTICATION_ERROR)) {
			$error = $request -> attributes -> get(SecurityContext::AUTHENTICATION_ERROR);
		} else {
			$error = $session -> get(SecurityContext::AUTHENTICATION_ERROR);
		}

		$username = null;
		if($this->get('security.context')->isGranted('ROLE_USER')) {
			$usernow = $this->container->get('security.context')->getToken()->getUser();
			$username = $usernow->getUsername();
		}
		$parameter = array ('last_username' => $session -> get(SecurityContext::LAST_USERNAME), 'error' => $error, );
		return $this -> render('AsiahSiklusBundle:User:login.html.twig', $parameter);
	}

	public function newAction(Request $request) {
		if ($this->get('security.context')->isGranted('ROLE_USER')){
			return $this -> redirect($this -> generateUrl('home'));
		}
		$pengguna = new User();
		$form = $this->createForm(new UserType(), $pengguna);
		//$this -> createFormBuilder($pengguna) -> add('name', 'text') -> add('username', 'text') -> add('password', 'password') -> add('email', 'email') -> getForm();

		if($request -> getMethod() == 'POST') {
			$form -> bindRequest($request);
			if($form -> isValid()) {
				$em = $this -> getDoctrine() -> getEntityManager()-> getRepository('AsiahSiklusBundle:User');
				$pengguna = $form -> getData();
				$em->saveNewUser($pengguna);

				return $this -> redirect($this -> generateUrl('home'));
			}
		}

		return $this -> render('AsiahSiklusBundle:User:new.html.twig', array('form' => $form -> createView(), ));
	} 
	public function detailAction($id){
		$em = $this->getDoctrine()->getEntityManager()->getRepository('AsiahSiklusBundle:User');
		$user = $em->getByUserName($id);
		if ($user === null)
			return $this->redirect($this->generateUrl('home'));
		return $this-> render('AsiahSiklusBundle:User:profile.html.twig', array('user' => $user));	
	}
	public function uploadAction()
	{
		$user = new User();
		$form = $this->createFormBuilder($user)
			->add('photo')
			->add('file')
			->getForm();
		$paramater =  array('form' => $form->createView());
		return $this->render('AsiahSiklusBundle:User:new.html.twig', $paramater);
	}
	public function sendMailAction()
	{
		$message = \Swift_Message::newInstance()
			->setSubject('Hello Email')
			->setFrom('riff.blend@gmail.com')
			->setTo('info@studioasiah.com')
			->setBody("testing ini adalah email");
		$this->get('mailer')->send($message);

		return new Response("testing");
	}
}                                           
